package Service.impl;

import Dao.EmployeeDao;
import Dao.impl.EmployeeDaoImpl;
import Domin.Employee;
import Service.EmployeeService;

import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {
    private final EmployeeDao employeeDao = new EmployeeDaoImpl();

    @Override
    public boolean addEmployee(String name, String dept, String pos, String role, String pwd) {
        if (name == null || name.trim().isEmpty()) return false;
        Employee emp = new Employee();
        emp.setEmpName(name);
        emp.setDepartment(dept);
        emp.setPosition(pos);
        emp.setRole(role);
        emp.setPassword(pwd);
        return employeeDao.insertEmployee(emp) != -1;
    }

    @Override
    public boolean login(String name, String pwd) {
        return employeeDao.findByEmpName(name)
                .filter(e -> e.getPassword().equals(pwd))
                .isPresent();
    }

    @Override
    public List<Employee> viewAllEmployees() {
        return employeeDao.getAllEmployees();
    }

    @Override
    public boolean updateEmployee(int empId, String name, String dept, String pos) {
        Employee emp = new Employee();
        emp.setEmpId(empId);
        emp.setEmpName(name);
        emp.setDepartment(dept);
        emp.setPosition(pos);
        return employeeDao.updateEmployee(emp);
    }

    @Override
    public boolean deleteEmployee(int empId) {
        return employeeDao.deleteEmployee(empId);
    }

    @Override
    public String getUserRole(String name) {
        return employeeDao.findByEmpName(name)
                .map(Employee::getRole)
                .orElse("guest");
    }
}